<html lang="en-US">

<head>
    
<title>Go1.17 新特性，优化抛出的错误堆栈 - 花落雨忧</title>

<meta property="og:title" content="Go1.17 新特性，优化抛出的错误堆栈 - 花落雨忧">



    



    
    <meta property="description" content="大家好，我是煎鱼。
平时在日常工程中，我们常常会用到异常恐慌（panic）的记录和追踪。最常见的就是，线上 panic 了之后，我们总想从中找到一些蛛丝马迹。
我们很多人是看 panic 是看他的调用堆栈。然后就开始猜，看代码。猜测是不是哪里写的有问题，就想知道 panic 是由什么参数引起的？
因为知道了诱发的参数，排查问题就非常方便了。为此在 Go1.17，官方对这块的调用堆栈信息展示进行了优 &amp;hellip;">
    <meta property="og:description" content="大家好，我是煎鱼。
平时在日常工程中，我们常常会用到异常恐慌（panic）的记录和追踪。最常见的就是，线上 panic 了之后，我们总想从中找到一些蛛丝马迹。
我们很多人是看 panic 是看他的调用堆栈。然后就开始猜，看代码。猜测是不是哪里写的有问题，就想知道 panic 是由什么参数引起的？
因为知道了诱发的参数，排查问题就非常方便了。为此在 Go1.17，官方对这块的调用堆栈信息展示进行了优 &amp;hellip;">
    






<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1">

<link rel="shortcut icon" href="https://www.lican.site/logo/logo.png" type="image/x-icon" />



<link rel="stylesheet" href="/css/style.min.css" />

<link rel="stylesheet" href="/css/reset.min.css" />




<script src="https://www.lican.site/js/highlight.min.js"></script>

<script>
hljs.configure({ ignoreUnescapedHTML: true })
hljs.highlightAll();
</script>


<script src="https://www.lican.site/js/jquery.min.js"></script>




<link href="https://www.lican.site/css/hugo-code.min.css" rel="stylesheet" />



    <style>
        .post-content img {
            max-width: 400px;
        }
    </style>
</head>

<body id="period" class="home blog">
    <a class="skip-content" href="#main">Press "Enter" to skip to content</a>
    <div id="overflow-container" class="overflow-container">
        <header class="site-header" id="site-header" role="banner">
    <div class="max-width">
        <div id="title-container" class="title-container">
            <div id="site-title" class="site-title"><a href="/">花落雨忧</a></div>
            <p class="tagline">思所及 力所行 方为真.</p>
        </div>
        
        <div id="menu-primary-container" class="menu-primary-container">
            <div class="icon-container">
            </div>
            <div id="menu-primary" class="menu-container menu-primary" role="navigation">
                <nav class="menu">
                    <ul id="menu-primary-items" class="menu-primary-items">
                        
                        
                        <li id="menu-item-0"
                            class="menu-item menu-item-type-custom menu-item-object-custom ">
                            <a href="/posts" aria-current="page" tabindex="0">首页</a></li>
                        
                        <li id="menu-item-1"
                            class="menu-item menu-item-type-custom menu-item-object-custom ">
                            <a href="/tech/" aria-current="page" tabindex="1">技术文档</a></li>
                        
                        <li id="menu-item-2"
                            class="menu-item menu-item-type-custom menu-item-object-custom ">
                            <a href="/article/" aria-current="page" tabindex="2">文章</a></li>
                        
                        <li id="menu-item-3"
                            class="menu-item menu-item-type-custom menu-item-object-custom ">
                            <a href="/project/" aria-current="page" tabindex="3">项目</a></li>
                        
                        <li id="menu-item-4"
                            class="menu-item menu-item-type-custom menu-item-object-custom ">
                            <a href="/about/" aria-current="page" tabindex="4">关于</a></li>
                        
                    </ul>
                </nav>
            </div>
        </div>
    </div>
</header>
        <div id="primary-container" class="primary-container">
            <div class="max-width">
                <section id="main" class="main" role="main">
                    <div id="loop-container" class="loop-container">
                        <div
                            class="post type-post status-publish format-standard hentry entry">
                            <article>
                                <div class="post-container">
                                    <div class="post-header">
                                        <h2 class="post-title">
                                            <a href="/posts/go/117-errorstack/">Go1.17 新特性，优化抛出的错误堆栈</a>
                                        </h2>
                                        
                                        <div class="post-byline">Published on
                                            <a class="date" href="javascript:;">2021/12/31</a>
                                            
                                            
                                            
                                            
                                            
                                        
                                    </div>
                                    <div class="post-content">
                                        <p>大家好，我是煎鱼。</p>
<p>平时在日常工程中，我们常常会用到异常恐慌（panic）的记录和追踪。最常见的就是，线上 panic 了之后，我们总想从中找到一些蛛丝马迹。</p>
<p>我们很多人是看 panic 是看他的调用堆栈。然后就开始猜，看代码。猜测是不是哪里写的有问题，就想知道 panic 是由什么参数引起的？</p>
<p>因为知道了诱发的参数，排查问题就非常方便了。为此<strong>在 Go1.17，官方对这块的调用堆栈信息展示进行了优化</strong>，使其可读性更友好。</p>
<h2 id="案例">案例</h2>
<p>结合我们平时所使用的 panic 案例。如下：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-golang" data-lang="golang"><span style="color:#66d9ef">func</span> <span style="color:#a6e22e">main</span>() {
	<span style="color:#a6e22e">example</span>(make([]<span style="color:#66d9ef">string</span>, <span style="color:#ae81ff">1</span>, <span style="color:#ae81ff">2</span>), <span style="color:#e6db74">&#34;煎鱼&#34;</span>, <span style="color:#ae81ff">3</span>)
}

<span style="color:#75715e">//go:noinline
</span><span style="color:#75715e"></span><span style="color:#66d9ef">func</span> <span style="color:#a6e22e">example</span>(<span style="color:#a6e22e">slice</span> []<span style="color:#66d9ef">string</span>, <span style="color:#a6e22e">str</span> <span style="color:#66d9ef">string</span>, <span style="color:#a6e22e">i</span> <span style="color:#66d9ef">int</span>) <span style="color:#66d9ef">error</span> {
	panic(<span style="color:#e6db74">&#34;脑子进煎鱼了&#34;</span>)
}
</code></pre></div><p>运行结果：</p>
<pre><code>$ go run main.go
panic: 脑子进煎鱼了

goroutine 1 [running]:
main.example(0xc000032758, 0x1, 0x2, 0x1073d11, 0x6, 0x3, 0xc000102058, 0x1013201)
	/Users/eddycjy/go-application/awesomeProject/main.go:9 +0x39
main.main()
	/Users/eddycjy/go-application/awesomeProject/main.go:4 +0x68
exit status 2
</code></pre><p>我们函数的入参是：<code>[]string、string、int</code>，核心关注到 <code>main.example</code> 方法的调用堆栈信息：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-golang" data-lang="golang"><span style="color:#a6e22e">main</span>.<span style="color:#a6e22e">example</span>(
    <span style="color:#ae81ff">0xc000032758</span>, 
    <span style="color:#ae81ff">0x1</span>, 
    <span style="color:#ae81ff">0x2</span>, 
    <span style="color:#ae81ff">0x1073d11</span>, 
    <span style="color:#ae81ff">0x6</span>, 
    <span style="color:#ae81ff">0x3</span>, 
    <span style="color:#ae81ff">0xc000102058</span>, 
    <span style="color:#ae81ff">0x1013201</span>
)
</code></pre></div><p>明明只是函数三个参数，却输出了一堆，对应起来非常的不清晰。</p>
<p>其实际对应是：</p>
<ul>
<li>slice：0xc000032758、0x1、0x2。</li>
<li>string：0x1073d11、0x6。</li>
<li>int：0x3。</li>
</ul>
<p>这里存在的问题是，看调用堆栈的人，还得必须了解基本数据结构（例如：slice、string、int 等），他才知道每个函数入参他对应拥有几个字段，才能知道其内存布局的结构，有一点麻烦。</p>
<p>并且从程序运行的角度来讲，这么水平平铺的方式，并不直观和准确。因为不同类型他是多个字段组合成结构才能代表一个类型。这不得还要人为估测？</p>
<h2 id="优化">优化</h2>
<p>终于，这一块的调用堆栈查看在 Go1.17 做了正式的改善。如下：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-golang" data-lang="golang"><span style="color:#960050;background-color:#1e0010">$</span> <span style="color:#a6e22e">go1</span><span style="color:#ae81ff">.17</span> <span style="color:#a6e22e">run</span> <span style="color:#a6e22e">main</span>.<span style="color:#66d9ef">go</span> 
<span style="color:#a6e22e">panic</span>: <span style="color:#a6e22e">脑子进煎鱼了</span>

<span style="color:#a6e22e">goroutine</span> <span style="color:#ae81ff">1</span> [<span style="color:#a6e22e">running</span>]:
<span style="color:#a6e22e">main</span>.<span style="color:#a6e22e">example</span>({<span style="color:#ae81ff">0x0</span>, <span style="color:#ae81ff">0xc0000001a0</span>, <span style="color:#ae81ff">0xc000034770</span>}, {<span style="color:#ae81ff">0x1004319</span>, <span style="color:#ae81ff">0x60</span>}, <span style="color:#ae81ff">0x0</span>)
	<span style="color:#f92672">/</span><span style="color:#a6e22e">Users</span><span style="color:#f92672">/</span><span style="color:#a6e22e">eddycjy</span><span style="color:#f92672">/</span><span style="color:#66d9ef">go</span><span style="color:#f92672">-</span><span style="color:#a6e22e">application</span><span style="color:#f92672">/</span><span style="color:#a6e22e">awesomeProject</span><span style="color:#f92672">/</span><span style="color:#a6e22e">main</span>.<span style="color:#66d9ef">go</span>:<span style="color:#ae81ff">9</span> <span style="color:#f92672">+</span><span style="color:#ae81ff">0x27</span>
<span style="color:#a6e22e">main</span>.<span style="color:#a6e22e">main</span>()
	<span style="color:#f92672">/</span><span style="color:#a6e22e">Users</span><span style="color:#f92672">/</span><span style="color:#a6e22e">eddycjy</span><span style="color:#f92672">/</span><span style="color:#66d9ef">go</span><span style="color:#f92672">-</span><span style="color:#a6e22e">application</span><span style="color:#f92672">/</span><span style="color:#a6e22e">awesomeProject</span><span style="color:#f92672">/</span><span style="color:#a6e22e">main</span>.<span style="color:#66d9ef">go</span>:<span style="color:#ae81ff">4</span> <span style="color:#f92672">+</span><span style="color:#ae81ff">0x47</span>
<span style="color:#a6e22e">exit</span> <span style="color:#a6e22e">status</span> <span style="color:#ae81ff">2</span>
</code></pre></div><p>新版本的调用堆栈的信息改变：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-golang" data-lang="golang"><span style="color:#a6e22e">main</span>.<span style="color:#a6e22e">example</span>(
    {<span style="color:#ae81ff">0x0</span>, <span style="color:#ae81ff">0xc0000001a0</span>, <span style="color:#ae81ff">0xc000034770</span>}, 
    {<span style="color:#ae81ff">0x1004319</span>, <span style="color:#ae81ff">0x60</span>}, 
    <span style="color:#ae81ff">0x0</span>
)
</code></pre></div><p>在 Go 语言以前的版本中，调用堆栈中的函数参数被打印成基于内存布局的十六进制值的形式，比较难以读取。</p>
<p>在 <strong>Go1.17 后，每个函数的参数都会被单独打印，并且以 “，” 隔开</strong>，复合数据类型（例如：结构体、数组、切片等）的参数会用大括号包裹起来，整体更易读。</p>
<p>其实际对应如下：</p>
<ul>
<li>slice：0x0, 0xc0000001a0, 0xc000034770。</li>
<li>string：0x1004319, 0x60。</li>
<li>int：0x0。</li>
</ul>
<p>这里也有一块细节要注意，你会发现 Go1.17 的函数参数的数量和以往的版本相比，少了。是因为函数的返回值存在于寄存器中，而不会存储到内存中。</p>
<p>因此函数返回值可能会是不准确的，所以也在新版本中也就不再打印了。</p>
<h2 id="总结">总结</h2>
<p>在 Go1.17 的新版本中，调用堆栈的函数参数的可读性得到了进一步的优化和调整，在后续的使用上可能能够带来一定的排错效率的提高。</p>
<p>你平时在借助调用堆栈排查问题呢，希望还获得什么辅助呢？</p>
<h2 id="参考">参考</h2>
<ul>
<li><a href="https://github.com/golang/go/issues/46708">GoTip: New Stack Trace Output Wrong</a></li>
<li><a href="https://github.com/golang/go/issues/45728">cmd/compile: bad traceback arguments</a></li>
<li><a href="https://mp.weixin.qq.com/s/AkJoXLlpSmw5vMZDpXoq5w">Go 1.17新特性详解：使用基于寄存器的调用惯例</a></li>
<li><a href="https://groups.google.com/g/golang-codereviews/c/JkhaLqHFReM?pli=1">doc/go1.17: reword &ldquo;results&rdquo; in stack trace printing</a></li>
</ul>


                                        
                                        
                                        
                                        <div class="rp4wp-related-posts">
                                            <h3>相关文章</h3>
                                            <ul>
                                                
                                                <li>
                                                    <div class="rp4wp-related-post-content">
                                                        <a
                                                            href="https://www.lican.site/posts/go/117-performance/">Go1.17 新特性，凭什么提速 5~10%？</a>
                                                    </div>
                                                </li>
                                                
                                                <li>
                                                    <div class="rp4wp-related-post-content">
                                                        <a
                                                            href="https://www.lican.site/posts/go/117-module-pruning-lazy/">Go1.17 新特性：对 Go 依赖管理的一把大剪刀</a>
                                                    </div>
                                                </li>
                                                
                                                <li>
                                                    <div class="rp4wp-related-post-content">
                                                        <a
                                                            href="https://www.lican.site/posts/go/site-history/">分久必合，golang.org 将成为历史！</a>
                                                    </div>
                                                </li>
                                                
                                                <li>
                                                    <div class="rp4wp-related-post-content">
                                                        <a
                                                            href="https://www.lican.site/posts/go/117-generics/">Go 1.17 支持泛型了？具体怎么用</a>
                                                    </div>
                                                </li>
                                                
                                                <li>
                                                    <div class="rp4wp-related-post-content">
                                                        <a
                                                            href="https://www.lican.site/posts/go/runtimepark/">Goroutine 一泄露就看到他，这是个什么？</a>
                                                    </div>
                                                </li>
                                                
                                            </ul>
                                        </div>
                                        
                                        
                                    </div>

                                    
                                    
                                    

                                    
                                    <div class="post-meta">
                                        
                                        
                                        <div class="post-tags">
                                            <ul>
                                            
                                            <li>
                                                <a href="/tags/go" title="View all posts tagged match">go</a>
                                            </li>
                                            
                                            </ul>
                                        </div>
                                        
                                        
                                        <nav class="further-reading">
                                            
                                            <div class="previous">
                                                <span>&lt;&lt; Prev</span>
                                                <a href="https://www.lican.site/posts/go/117-performance/"
                                                    rel="prev">Go1.17 新特性，凭什么提速 5~10%？</a> </div>
                                            
                                            
                                            <div class="next">
                                                <span>Next >></span>
                                                <a href="https://www.lican.site/posts/go/goroutine-27/">会诱发 Goroutine 挂起的 27 个原因</a> 
                                            </div>
                                            
                                        </nav>
                                    </div>
                                    
                                    

                                    
                                    
                                    

                                </div>
                            </article>
                        </div>
                    </div>
                </section>
                <aside class="sidebar sidebar-primary" id="sidebar-primary" role="complementary">
    <h1 class="screen-reader-text">Sidebar</h1>
    

    
    
    <section id="text-2" class="widget widget_text">
        <div class="textwidget">
            
            <div id="profile">
                <div id="profile_picture"><img src="https://www.lican.site/logo/logo.png"></div>
                <div id="profile_intro">
                    <p><span class="name">Lican</span></p>
                    <p class="intro">全栈开发者，爱好造轮子。</p>
                </div>
            </div>
            
            <p>
                <script type="text/javascript">
                    (function ($) {
                        $(document).ready(function () {
                            var menuPrimaryContainer = $('#menu-primary-container');
                            var profile = $('#text-2');
                            $('#toggle-navigation').click(function () {
                                if (menuPrimaryContainer.hasClass('open')) {
                                    profile.removeClass('open');
                                } else {
                                    profile.addClass('open');
                                }
                            });
                        });
                    })(jQuery);
                </script>
            </p>
        </div>
    </section>
    
    
    
    
    
    <section id="text-5" class="widget widget_text">
        <h2 class="widget-title">开源项目</h2>
        <div class="textwidget">
            <div id="projects" style="line-height: 22px;">
                
                <a href="https://github.com/idoubi/gonews"
                    target="_blank">gonews</a>: &nbsp;Daily news for golang<br>
                
                <a href="https://github.com/idoubi/sql2struct"
                    target="_blank">sql2struct</a>: &nbsp;Generate go struct according to SQL<br>
                
                <a href="https://github.com/idoubi/goz"
                    target="_blank">goz</a>: &nbsp;Request library used in golang<br>
                
        </div>
    </section>
    
    

    
    
    
    
    

    
    
    

    
    
    

    
    
    
    
</aside>
            </div>
        </div>

        <footer id="site-footer" class="site-footer" role="contentinfo">
    <div class="max-width">
    </div>
    <div class="footer">
        <div id="footercontent">
            © lican.vip All rights reserved<br/>
            Built with Hugo Theme <a href="https://github.com/idoubi/hugo-theme-period" target="_blank">Period</a>
        </div>
    </div>
</footer>

<script>
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?e8351b6d4626d5881d439ea1f6184baa";
      var s = document.getElementsByTagName("script")[0]; 
      s.parentNode.insertBefore(hm, s);
    })();
</script>
    
    
    </div>
    
</body>

</html>